package com.hzjh.h5mall.adapter.register

import android.text.Editable
import android.text.TextWatcher
import android.view.Gravity
import com.hzjh.base.ext.R
import com.hzjh.base.frame.adapter.BaseBindingVH
import com.hzjh.base.utils.other.CheckUtils
import com.hzjh.h5mall.bean.register.MnemonicWordBean
import com.hzjh.h5mall.databinding.ItemMnemonicBinding

//fromPage=0生成页面  fromPage=1校验页面
class MnemonicAdapter(binding:ItemMnemonicBinding,
                      private val fromPage:Int? = 0,
                      private val inputWord: ((MnemonicWordBean,String) -> Unit)?=null) : BaseBindingVH<MnemonicWordBean, ItemMnemonicBinding>(binding) {
    override fun onBind(model: MnemonicWordBean, position: Int) {
        binding.tvNumItem.text = "${position+1}."
        if(binding.etWordItem.tag is TextWatcher){
            binding.etWordItem.removeTextChangedListener(binding.etWordItem.tag as TextWatcher)
        }
        binding.etWordItem.setText(model.name)
        if(CheckUtils.isEmpty(model.name)&&fromPage==1){
            binding.etWordItem.isEnabled=true
            binding.etWordItem.setBackgroundResource(R.drawable.bg_rect_stroke_red_8dp)
            val watcher = object : TextWatcher {
                override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

                }

                override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

                }

                override fun afterTextChanged(p0: Editable?) {
                    inputWord?.invoke(model,p0.toString())
                }
            }
            binding.etWordItem.addTextChangedListener(watcher)
            binding.etWordItem.tag = watcher
        }else{
            binding.etWordItem.isEnabled=false
            binding.etWordItem.setBackgroundResource(R.drawable.bg_rect_red_8dp)
        }
        when(position%3){
            0 -> {
                binding.llContentItem.gravity = Gravity.START
            }
            1 -> {
                binding.llContentItem.gravity = Gravity.CENTER
            }
            2 -> {
                binding.llContentItem.gravity = Gravity.END
            }
        }
    }
}